#include "lcd.h"
#include <Arduino.h>
#include <TFT_eSPI.h>       // Hardware-specific library
#include "adc.h"

TFT_eSPI tft = TFT_eSPI();  // Invoke custom library

Buttom_Unit g_buttom[20] = {0};

void Ui_SetButtom(int index, int status)
{
  if (index >= 12) {
    return;
  }

  int color = 0;
  if (status == 0) {
    color = TFT_BLACK;
  } else {
    color = TFT_WHITE;
  }
  tft.fillRect(g_buttom[index].x, g_buttom[index].y, g_buttom[index].w, g_buttom[index].h, color);  
}

void Ui_SetWifiStatus(int status)
{
  static int st = 0;
  if (status > 1) {
    status = 1;
  }
  char *text[] = {"UnConnect", "Connect"};
  tft.setTextColor(TFT_BLACK);
  tft.drawString(text[st], 80, 0);

  tft.setTextColor(TFT_WHITE);
  tft.drawString(text[status], 80, 0);
  st = status;  
}

void Ui_SetWifiInfo(char* info)
{
  tft.setTextColor(TFT_BLACK);
  tft.drawString(info, 60, 20);

  tft.setTextColor(TFT_WHITE);
  tft.drawString(info, 60, 20);
}

void Ui_SetServerStatus(int status)
{
  static int st = 0;
  if (status > 1) {
    status = 1;
  }
  char *text[] = {"UnConnect", "Connect"};
  tft.setTextColor(TFT_BLACK);
  tft.drawString(text[st], 80, 40);

  tft.setTextColor(TFT_WHITE);  
  tft.drawString(text[status], 80, 40);
  st = status;  
}

void Ui_SetConnectStatus(int status)
{
  static int st = 0;
  if (status > 1) {
    status = 1;
  }
  char *text[] = {"Offline", "Online"};
  tft.setTextColor(TFT_BLACK);
  tft.drawString(text[st], 80, 60);

  tft.setTextColor(TFT_WHITE);  
  tft.drawString(text[status], 80, 60);
  st = status;
}

char oldtext[50] = {0};
u16 displayLen = 0; 
void Ui_SetAdcValue(int value)
{
  char text[50] = {0};
  int len = sprintf(text, "%d", value);
  if (displayLen != 0) {
    tft.setTextColor(TFT_BLACK);
    tft.drawString(oldtext, 100, 105);
  }

  tft.setTextColor(TFT_WHITE);  
  tft.drawString(text, 100, 105);
  memcpy(oldtext, text, len);
  displayLen = 1;
}

void Ui_Init(void)
{
  tft.drawString("WIFI", 0, 0);
  tft.drawString("IP", 0, 20);
  tft.drawString("Server", 0, 40);
  tft.drawString("State", 0, 60);
  tft.drawString("AdcValue", 0, 105);
  Ui_SetWifiStatus(UNCONNECT);
  Ui_SetServerStatus(UNCONNECT);
  Ui_SetConnectStatus(OFLINE);
  int x_index = 0;
  int y_index = 80;
  int w = 20; 
  int h = 20;  
  int space = 2;
  for (int i = 0; i < 12; i++) {
      tft.fillRect(x_index, y_index, w, h, TFT_BLUE);
      tft.fillRect(x_index + space, y_index + space, w - space*2, h - space*2, TFT_BLACK);
      g_buttom[i].x = x_index + space + 1;
      g_buttom[i].y = y_index + space + 1;
      g_buttom[i].w = w - 2 * space - 2;
      g_buttom[i].h = h - 2 * space - 2;
      x_index += w + 2;
      // printf("%d %d %d %d\r\n", g_buttom[i].x,  g_buttom[i].y, g_buttom[i].w, g_buttom[i].h);
  } 
  // Ui_SetButtom(5, 1);
}

void LcdInit(void)
{
  tft.begin();
  tft.setRotation(1);	// landscape
  tft.fillScreen(TFT_BLACK);
  pinMode(12, OUTPUT);
  digitalWrite(12, 1);
  tft.setTextColor(TFT_WHITE);
  tft.setTextSize(2);  
  Ui_Init();
}


void LcdTask(u32 tick)
{
  if ((tick % 300) == 0) {
    Ui_SetAdcValue(AdcGetValue());
  }
}